<?php
/**
 * Model_InfoCommon
 *
 * @package    model
 */
class Model_Mass extends Model_Table {

	public $token;
	public $toUserStr;
	public $model = 'openid';
	public $filter;
	public $member_num;
	public $url;
	
	
	public function __construct () {
		parent::__construct();
		$this->token	 = Model_Com::getWxToken();
	}
	

	//
	public function send_format ($type,$info) {
			
		$this->toUserStr = $this->get_member_list();
		
		$this->url 		 = $this->get_url();
		
		$action		= 'send_'.$type;
		
		$content	= $this -> $action($info);
		
		$return = Model_PostCurl::wx_send($this->url,$content);

		return $return['errmsg'];
		
	}
	
	//模式选择 根据group或openid发送  (group,openid)
	public function set_model ($model = 'openid') {
		$this->model = $model;
		return $this;
	}
	
	//群发文字
	public function send_text ($text,$model = '') {
		
		$json_str = $this->toUserStr.'
					   "text":{
						  "content":"'.$text['info_intro'].'"
					   },
						"msgtype":"text"
					}';
		
		return $json_str;
	}
	
	//群发图文
	public function send_mpnews ($multi,$model = '') {
		
		if ($model == 'cus_service_model') {
			
			$data		= $this->get_multi_media_id($multi,$model);
			
			$json_str	= $this->toUserStr.
						' "msgtype":"news",
						  "news":
								'.$data.'
						}';
							
		
			
		} else {
		
			$multi_media_id = $this->get_multi_media_id($multi);
			
			$json_str = $this->toUserStr.'
					   "mpnews":{
							  "media_id":"'.$multi_media_id.'"
						   },
						"msgtype":"mpnews"
						}';
		}
		return $json_str;
	}
	
	//群发图片
	public function send_image ($image,$model = '') {
		
		$image_media_id = Model_Com::upImageToWx(ROOT_PATH.'data/image_c/'.$image['path']);
		
		$json_str = $this->toUserStr.'
					   "image":{
								  "media_id":"'.$image_media_id.'"
							},
						"msgtype":"image"
						}';
		return $json_str;
	}
	
	
	//得到用户列表
	public function get_member_list () {
		
		if ($this->model == 'group') {
			
			return $json_str_group = $this->get_group_str();
			
		} else {
			
			return $json_str_openid = $this->get_opendid_str();	
		}
	}

	//得到条件
	public function get_filter () {
		
		$filter = array();
		$_POST['sex']		? $filter['sex'] 		= $_POST['sex'] : '';
		$_POST['group_id']		? $filter['group_id']		= $_POST['group_id'] : '';
		$_POST['is_bind']	? $filter['is_bind']	= $_POST['is_bind'] : '';
		$_POST['provin']	? $filter['province'] 	= substr($_POST['provin'],0,strlen($_POST['provin'])-3) : '';
		$_POST['city']		? $filter['city'] 		= substr($_POST['city'],0,strlen($_POST['city'])-3) : '';
		$_POST['label']		? $filter['label'] 		= $_POST['label'] : '';
		
		$this->filter = $filter;
	}
	
	//
	public function get_sql () {
		
		$this->get_filter();
		
		if (!empty($this->filter)) {
			
			$where = '';
			
			$label = $this->filter['label'];
			if ($label) {
				$where .= 'ml.mem_id=m.id and ml.lab_id in ('.$label.') and ';
			}
			unset($this->filter['label']);
			
			if (!empty($this->filter)) {
				foreach ($this->filter as $k=>$val) {
					$where .= "m.$k='$val' and ";
				}
			}
			
			return rtrim($where,'and ').'group by m.fromuser';
			
		} else {
			return $sql = "m.id > 0 group by m.fromuser";
		}
	}
	
	public function get_opendid_str ($model = '') {
		
		if ($model == 'cus_service_model') {
			
			$json_str_member = '{"touser":"'.$this->fromuser.'",';
			
		} else {
			
			$where = $this->get_sql();
			
			$table = new Model_Member('member m,member_label ml');
			
			$memArray = $table->findAll($where,'m.fromuser');
			
			if (empty($memArray)) {
				die(json_encode('当前条件下无符合用户'));
			}
			
			$this->member_num = count($memArray);
			$member_list = '';
			foreach ($memArray as $val){
				$member_list .= '"'.$val['fromuser'].'",';
			}
			
			$json_str_member =  '{
							   "touser":[
							   '.rtrim($member_list,',').'
							   ],';
			
			
		}
								   
		return $json_str_member;
	}
	
	public function get_group_str () {
		$group_id	= 	isset($_POST['to_group']) ? (int)$_POST['to_group'] : '';
		$is_to_all	=	$group_id=='' ? true : false ;
		
		$json_str_member = '{
								   "filter":{
									  "is_to_all":'.$is_to_all.'
									  "group_id":"'.$group_id.'"
								   },';
		return $json_str_member;
	}
	
	
	public function get_url($model = ''){
		
		if ($model == 'cus_service_model') {
			
			$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->token;
			
		} else {
			$url_element = $this->model == 'group' ? 'sendall' : 'send' ;
			$url = "https://api.weixin.qq.com/cgi-bin/message/mass/$url_element?access_token=".$this->token;
		}
		
		return $url;
	}
	
		
	public function get_multi_media_id ($multi,$model = '') {
		
		global $app_path;
		
		$data = '{"articles": [	';
		
		if ($multi['info_type'] == 2) {			//单图文
		
			if ($multi['info_url']) {
				$url = $multi['info_url'];
			} else {
				$url = $app_path."/mobile/info.php?t=single&id=" . $multi['id'];
			}
		
			if ($model == 'cus_service_model') {
				
				$data .= '{"title":"'.$multi['info_title'].'",
							 "description":"'.$multi['info_desc'].'",
							 "url":"'.$url.'",
							 "picurl":"'.$app_path.'/data/image_c/'.$multi['info_pic'].'"
						 }';
				
			} else {
				
				$media_id = Model_Com::upImageToWx('../../data/image_c/'.$multi['info_pic']);
		
				$data .= ' {
							 "thumb_media_id":"'.$media_id.'",
							 "author":"",
							 "title":"'.$multi['info_title'].'",
							 "content_source_url":"'.$url.'",
							 "content":"'.$multi['info_intro'].'",
							 "digest":"'.$multi['info_desc'].'",
							 "show_cover_pic":"0"
						 }';
					 
			}
					 
		} else if ($multi['info_type'] == 3){		//多图文
			
			set_time_limit(0);
			
			$where = "info_common_id=$multi[id]";
			$multiArray = Model_Table::get('InfoCommonDetail')->findAll($where,'*','order_num');
	
			
			if ($model == 'cus_service_model') {
				
				foreach ($multiArray as $detail) {
					
					if ($detail['url']) {
						$url = $detail['url'];
					} else {
						$url = $app_path."/mobile/info.php?t=multi&id=" . $detail['id'];
					}
					
					$data .= '{"title":"'.$detail['title'].'",
							 "description":"'.$detail['desc'].'",
							 "url":"'.$url.'",
							 "picurl":"'.$app_path.'/data/image_c/'.$detail['pic'].'"
						 },';
				}
				
			} else {
				
				foreach ($multiArray as $detail) {
					$media_id = Model_Com::upImageToWx('../../data/image_c/'.$detail['pic']);
					
					if ($detail['url']) {
						$url = $detail['url'];
					} else {
						$url = $app_path."/mobile/info.php?t=multi&id=" . $detail['id'];
					}
					
					$data .= '{
							 "thumb_media_id":"'.$media_id.'",
							 "author":"",
							 "title":"'.$detail['title'].'",
							 "content_source_url":"'.$url.'",
							 "content":"'.$detail['msg_content'].'",
							 "digest":"'.$detail['desc'].'",
							 "show_cover_pic":"0"
						 },';
				}
			}
			
			$data = rtrim($data,',');
			
		}
		$data.=']}';
		
		//dump($data);
		
		if ($model) {
			
			return $data;
			
		} else {
			$url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=".$this->token;
		
			$returnBack = Model_PostCurl::wx_send($url,$data);
			return $returnBack['media_id'];
		}
	}
	
	
	//发送客服信息
	public function send_customer_service($type,$id,$fromuser) {
		
		$this->fromuser = $fromuser;
		
		$url 	= $this->get_url('cus_service_model');
		
		$data	= self::get_service_data($type,$id);
		
		return $return = Model_PostCurl::wx_send($url,$data);

	}

	//得到客服消息
	public function get_service_data ($type,$id) {
		
		$table	= $type == 'image' ? new Model_Images : new Model_InfoCommon;
		
		$info	= $table->find($id);
	
		if ($info['id']) {
			
			$this->toUserStr = $this->get_opendid_str('cus_service_model');
			
			$action			 = 'send_'.$type;
		
			return $content	 = $this -> $action($info,'cus_service_model');
			
		} else {
			
			ajax_feedback(0,'local330','数据丢失，请尝试刷新后再次操作');
			
		}
	}
}

